/**   
 * Copyright © 2019 eSunny Info. Tech Ltd. All rights reserved.
 * 
 * @Description:TODO(功能)
 * @className CategorysDaoImpl.java
 * @Package: com.erp.dao 
 * @author: yj   
 * @date: 2019年1月17日 上午9:45:46 
 */
package com.erp.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.erp.po.Categorys;
import com.erp.util.Page;

/**
 * @className CategorysDaoImpl.java
 * @author yj
 * @Description:TODO(功能)
 * @Date 2019年1月17日
 */
public class CategorysDaoImpl extends BaseDao implements CategorysDao {

	// -------------------------------- Categorys
	// ---------------------------------------------
	/*
	 * ( 查询 ( 查询 Categorys <类别表> 全部 )) 待完成--- 分页
	 */
	
	@Override
	public ResultSet selectCategorysByPage(Connection conn, Page<Categorys> page) throws SQLException {
		// String sql = "select * from categorys ORDER BY categoryID limit ? , ? "; // mysql
		String sql = "select * from (select rownum rn,categorys.* from categorys where rownum <= ? ORDER BY categoryID ) temp where temp.rn > ? "; // oracle
		// Object[] obj = { (page.getCurrentPage() - 1) * page.getPageSize(), page.getPageSize() }; // mysql
		Object[] obj = { (page.getCurrentPage() * page.getPageSize()), (page.getCurrentPage() - 1) * page.getPageSize() }; // oracle
		return super.selectData(conn, sql, obj);
	}
	/* (查询 ( 查询 Categorys 总记录数 ))
	 *
	 */
	@Override
	public ResultSet selectCategorysByTotal(Connection conn) throws SQLException {
		String sql = "select count(*) from categorys";
		return super.selectData(conn, sql, null);
	}

	/*
	 * (增加 ( 增加 类别 ))
	 * 
	 */
	@Override
	public int insertCategorys(Connection conn, Categorys categorys) throws SQLException {
		// String sql = "insert into categorys (category_name,category_desc) values(?,?)";	// mysql
		String sql = "insert into categorys (categoryID,category_name,category_desc) values(CATEGORYS_SEQ.nextval,?,?)";	// oracle
		Object[] obj = { categorys.getCategoryName(), categorys.getCategoryDesc() };
		return super.updateData(conn, sql, obj);
	}
	/* (查询 (是否有添加重名的商品))
	 * 
	 */
	@Override
	public ResultSet selectCategorysByInsert(Connection conn, Categorys categorys) throws SQLException {
		String sql = "select * from categorys where category_name = ? ";
		return super.selectData(conn, sql, categorys.getCategoryName());
	}
	

	/*
	 * (修改 ( 修改 类别 名称 ,简介 ))
	 * 
	 */
	@Override
	public int updateCategorysById(Connection conn, Categorys categorys) throws SQLException {
		String sql = "update categorys set category_name = ? , category_desc = ? where categoryID = ? ";
		Object[] obj = { categorys.getCategoryName(), categorys.getCategoryDesc(), categorys.getCategoryId() };
		return super.updateData(conn, sql, obj);
	}

	
	@Override
	public int deleteCategorysById(Connection conn, int categoryId) throws SQLException {
		String sql = "delete from categorys where categoryID = ? ";
		return super.updateData(conn, sql, categoryId);
	}
	
	
	/* (查询 category)
	 * 
	 */
	@Override
	public ResultSet selectCategorys(Connection conn) throws SQLException {
		String sql = "select * from categorys ORDER BY categoryID ";
		return super.selectData(conn, sql, null);
	}
	
	
	/* (根据id查询 category)
	 * 
	 */
	@Override
	public ResultSet selectCategoryById(Connection conn, int id) throws SQLException {
		String sql = "select * from categorys where categoryID = ? ";
		return super.selectData(conn, sql, id);
	}
	
	/**
	 * 根据名称查询 category
	 */
	@Override
	public ResultSet selectCategoryByName(Connection conn, String cateName)  throws SQLException {
		String sql = "select * from categorys where category_name = ? ";
		return super.selectData(conn, sql, cateName);
	}
	/* 
	 * 根据条件查询 ( 根据条件查询 Categorys <类别表> 全部 ) ( 分页查询 )
	 */
	@Override
	public ResultSet selectCategorysByCondition(Connection conn, Categorys cate, Page<Categorys> page)
			throws SQLException {
		ArrayList list = new ArrayList();
		String sql = "select * from (select rownum rn,categorys.* from categorys where rownum <= ? ";
		list.add(page.getCurrentPage() * page.getPageSize());
		if(cate.getCategoryName() != null) {
			sql = sql.concat(" and category_name like ? ");
			list.add("%"+cate.getCategoryName()+"%");
		}
		sql = sql.concat(" ORDER BY categoryID ) temp where temp.rn > ? ");
		list.add((page.getCurrentPage() - 1) * page.getPageSize());
		return super.selectData(conn, sql, list.toArray());
	}
	/* 
	 * 根据条件查询 ( 根据条件查询 Categorys 总记录数 )
	 */
	@Override
	public ResultSet selectConditionByTotal(Connection conn, Categorys cate) throws SQLException {
		ArrayList list = new ArrayList();
		String sql = "select count(categoryID) from categorys where 1=1 ";
		if(cate.getCategoryName() != null) {
			sql = sql.concat(" and category_name like ? ");
			list.add("%"+cate.getCategoryName()+"%");
		}
		return super.selectData(conn, sql, list.toArray());
	}


	

}
